const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcryptjs');
mongoose.set('useCreateIndex', true);
const utils = require('../utils/utils');
const deviceSchema = new Schema({
  devicetoken: String,
  platform: Number//1 ios, 2 android, 3 winphone, 4 others
});

const imageSchema = new Schema({
  origin: {type: String, default: ''},
  medium: {type: String, default: ''},
  small: {type: String, default: ''},
  uploadedTime: {type: Number, default: 0}
}, {
  toObject: {
    transform: function(doc, ret) {
      delete ret._id;
    }
  },
  toJSON: {
    transform: function(doc, ret) {
      delete ret._id;
    }
  }
});

const avatarSchema = new Schema({
  origin: {type: String, default: ''},
  uploadTime: {type: Number, default: 0},
  medium: {type: String, default: ''},
  small: {type: String, default: ''}
}, {
  toObject: {
    transform: function(doc, ret) {
      delete ret._id;
    }
  },
  toJSON: {
    transform: function(doc, ret) {
      delete ret._id;
    }
  }
});

const UserSchema = new Schema({
  blocked: [String],//被locked userCode集合
  mobile: {type: String, default: ''},
  countryCode: {type: Number, default: 65},
  region: {type: String, default: ''},
  language: {type: String, default: ''},
  birthday: {type: Number, default: 0},
  whatsup: {type: String, default: ''},
  name: {type: String, default: ''},
  nickName: {type: String, default: ''},
  gender: Number,
  facebook: {
    facebookId: String,
    expired: {type: Number, default: 0},
    token: String,
    picUrl: String
  },
  password: {
    type: String,
    require: false,
    default: 'defaultpassword' + Date.now()
  },
  device: {type: deviceSchema, default: {}},
  //账号种类
  accountType: {
    type: String,
    default: 'mobile',
    enum: ['mobile', 'facebook', 'wechat', 'others', 'nologin', 'normal']
  },
  isEnabled: {type: Boolean, default: false},
  roleType: {type: Number, default: 0},
  avatar: {type: imageSchema, default: emptyImage},
  email: {type: String, default: ''},
  logintime: {type: Number, default: 0}
}, {
  usePushEach: true
});

// 添加用户保存时中间件对password进行bcrypt加密,这样保证用户密码只有用户本人知道
UserSchema.pre('save', function(next) {
  var user = this;
  user.publishedResources.isLive = (user.tokbox.status === 'on');
  if(this.isModified('password') || this.isNew){
    bcrypt.genSalt(10, function(err, salt) {
      if(err){
        return next(err);
      }
      bcrypt.hash(user.password, salt, function(err, hash) {
        if(err){
          return next(err);
        }
        user.password = hash;
        next();
      });
    });
  } else {
    return next();
  }
});

// 校验用户输入密码是否正确
UserSchema.methods.comparePassword = function(passw, cb) {
  bcrypt.compare(passw, this.password, (err, isMatch) => {
    if(!this.isEnabled){
      return cb('user disabled');
    }
    if(err){
      return cb(err);
    }
    cb(null, isMatch);
  })
}

UserSchema.methods.getCreator_nopromise = function() {
  return {
    nickName: this.nickName,
    name: this.name,
    userCode: this.userCode,
    avatar: this.avatar,
    followingCount: this.following.length,
    followerCount: this.follower.length,
    videoCount: this.videoCount ? this.videoCount : 0,
    momentCount: this.momentCount ? this.momentCount : 0
  }
}

module.exports = {
  user: mongoose.model('user', UserSchema),

}

